import pandas as pd
import rqdatac as rq
from datetime import timedelta
from ml.utility import make_domaint_time_dict, sec_to_wh_ind
from ml.factor.dehydration import dehydration
from ml.factor.f1_index_qr import get_factor_df as f1_get_factor
from ml.factor.f2_ma_deviation import get_factor_df as f2_get_factor
from ml.factor.f3_atr import get_factor_df as f3_get_factor
from ml.factor.f4_volume import get_factor_df as f4_get_factor
from ml.factor.f5_position import get_factor_df as f5_get_factor
from ml.factor.f6_time import get_factor_df as f6_get_factor
from ml.factor.f7_pattern import get_factor_df as f7_get_factor
from ml.factor.f8_candle import get_factor_df as f8_get_factor


if __name__=="__main__":

    if not rq.initialized():
        rq.init("13570866213", "39314656")
    dehydration_df = []
    for sec_id in ["rb", "hc", "ss", "ru", "ni", "sn", "ag", "br", "sp",
                   "TA", "MA", "SA", "UR", "FG", "CF", "RM", "SM", "SF", "SR",
                   "i", "y", "p", "eg", "eb", "j", "v", "a", "jd", "lh", "l", "pp", "m", "c", "cs"]:
        if sec_id not in sec_to_wh_ind:
            print(sec_id, "not processed")
            continue
        symbol_time_dict = make_domaint_time_dict(sec_id)
        prices_df = {}

        for symbol, time_dict in symbol_time_dict.items():
            prices = rq.get_price(symbol, start_date="20220801", end_date="20231116", frequency="1m",
                                  fields=["close", "high", "low", "open", "volume", "open_interest"], expect_df=False)
            prices.index -= timedelta(minutes=1)
            for get_factor in [f1_get_factor, f2_get_factor, f3_get_factor, f4_get_factor, f5_get_factor,
                               f6_get_factor, f7_get_factor, f8_get_factor]:
                prices = get_factor(sec_id, prices)
            prices["symbol"] = symbol

            prices["y1"] = (prices["close"].shift(-10) - prices["close"]) / prices["close"]
            prices["y2"] = (prices["close"].shift(-30) - prices["close"]) / prices["close"]

            prices = prices[time_dict["start"]: time_dict["last"]]
            prices_df[symbol] = prices

        result = pd.concat(list(prices_df.values()))

        result.to_csv(f"D:\daily work\ml\\raw\\{sec_id}_prices.csv")
        clear_df = result.drop(
            ["close", "high", "low", "open", "volume", "open_interest", "wh_ind", "date", "time", "symbol"], axis=1)
        clear_df = dehydration(clear_df)
        dehydration_df.append(clear_df)
    df = pd.concat(dehydration_df)

